<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use Carbon\Carbon;
use Carbon\Exceptions\Exception;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Statistics extends Backend
{

    /**
     * Roomclose模型对象
     * @var \app\admin\model\Roomclose
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function index()
    {
        function array_group_by($array, $keys)
        {
            if(!is_array($keys) || count($keys) == 1)
            {
                $key = is_array($keys) ? array_shift($keys) : $keys;

                return array_reduce($array, function($tmp_result, $item) use ($key)
                {
                    $tmp_result[$item[$key]][] = $item;

                    return $tmp_result;
                });
            }
            else
            {
                $keys = array_values($keys);

                $result = array_group_by($array, array_shift($keys));

                foreach ($result as $k=>$value)
                {
                    $result[$k] = array_group_by($value, $keys);
                }

                return $result;
            }
        }

        $starttime = Carbon::now()->startOfMonth()->toDateTimeString();
        $endtime = Carbon::now()->endOfMonth()->toDateTimeString();


        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {

            $stores = \app\admin\model\Store::all();

            //初始化
            $output = [];
            foreach ($stores as $v) {
                $output[$v->id] = [
                    'store_name' => $v->store_name,
                    'reserve_num' => 0,
                    'reserve_done_num' => 0,
                    'reserve_cancel_num' => 0,
                    'total_reserve_hours' => 0,
                    'total_reserve_fee' => 0,
                ];
            }

            $filter = $this->request->get("filter", '');
            $filter = (array)json_decode($filter, true);
            $filter = $filter ? $filter : [];

            $reserve = new \app\admin\model\Reserve();

            if (isset($filter['statics_time'])) {
                list($starttime, $endtime) = explode(' - ', $filter['statics_time']);
            }
            $reserve->where('endtime', '>', $starttime)
                ->where('endtime', '<', $endtime);

            $records = $reserve->select();


            if ($records) {
                $groupedArray = array_group_by($records, 'store_id');
            } else {
                $groupedArray = [];
            }

            foreach ($groupedArray as $k => $storeData) {
                foreach ($storeData as $v) {
                    $output[$v->store_id]['reserve_num'] ++;
                    if (2 == $v->re_status) {
                        $output[$v->store_id]['reserve_cancel_num'] ++;
                    } else {
                        $output[$v->store_id]['reserve_done_num'] ++;
                    }
                    $output[$v->store_id]['total_reserve_hours'] += strtotime($v->endtime) - strtotime($v->starttime);
                    $output[$v->store_id]['total_reserve_fee'] += $v->fee;
                }
            }

            $output = array_map(function ($e) {
                $e['total_reserve_hours'] = intval($e['total_reserve_hours'] / 3600);
                return $e;
            }, $output);

            $result = array("total" => count($output), "rows" => array_values($output));

            return json($result);
        }
        $this->view->assign('starttime', $starttime);
        $this->view->assign('endtime', $endtime);
        return $this->view->fetch();
    }



}
